
const State={
    stand: 1,
    attack: 2,
    hurt: 3,
    die: 4
}

cc.Class({
    extends: cc.Component,

    properties: {
        //最大血量
        maxHP: 5,
        //血条节点
        hpBar:{
            default: null,
            type: cc.Node,
        },
    },

    onLoad () {
        
        //当前血量
        this.hp = this.maxHP;
        //受击开关
        this.isHit = false;
        //攻击冷却时间
        this.actTimes = 0;
        //移动速度
        this.speedX = 100;
        //击倒速度
        this.hitDown = 80;
        //设置怪物坐标
        this.sp = cc.v2(0,0);
        this.tt = 0;
        
        this.enemyState = State.stand;
        
        //移动方向开关
        this.moveLeft = false;
        this.moveRight = false;
  
        this.anim = 'idle';
        this.music = '';

        this.emAnim = this.node.getChildByName('body').getComponent(cc.Animation);
        this.rlv = this.node.getComponent(cc.RigidBody );
        this.bar = this.hpBar.getComponent(cc.ProgressBar);
        this.enemyMusic = this.node.getComponent(cc.AudioSource);
    

        this.rlv.fixedRotation = true; //固定初始旋转值
    
        this.rolesNode = cc.find("Canvas/role");
    //    this.amountNode = cc.find("Canvas/bg/ShowNum/item01");

        // var actionBy = cc.jumpBy(2, cc.v2(145, -227), 50, 5);
        // this.node.runAction(actionBy)

        this.emAnim.on('finished', (e, data) => {
           
            //console.log(data.name);
            if (data.name == 'hurt') {
                this.isHit = false;
                this.enemyState = State.stand;
                this.playMusic('punch2');
                this.setAnim('idle');
                if (this.hp ==0) {
                    this.death(); 
                }
               // console.log(this.hp);
            }else if (data.name == 'attack') {

                //this.isAttack = false;
                this.setAnim('idle');
                this.enemyState = State.stand;
            }

            if (this.hp == 0 && data.name == 'die') {
               
                this.hpBar.opacity = 0;
               
                this.scheduleOnce(function() {
                    // 这里的 this 指向 component
                    this.node.destroy();
                }, 1.5);
               
            }
           
        });
        
    },

    // showKillAmo(string){
    //     this.numLable = this.amountNode.getComponent(cc.Label);
    //     this.numLable.string = string;
    // },
    // start () {

    // },

    playMusic(music){
        this.enemyMusic.play(music);
    },
 
    setAnim(anim){
        if (this.anim == anim) {
            return;
        }
        this.anim = anim;
        this.emAnim.play(anim);
    },

    attack(){

        // var attX = 200;
        // var attY = 100; 
        if (this.actTimes >0) {
            //console.log(this.actTimes);
            return;
        }
     //   this.enemyState = State.attack;
        //this.isAttack = true;
        this.lv = this.rlv.linearVelocity;

        if (this.node.scaleX == 1) {
            var det = this.node.scaleX;
            this.lv.x = det * 100;           
                  
        }else if (this.node.scaleX == -1) {
            var det = this.node.scaleX
            this.lv.x = det * 100;    
           
        }
      
        this.actTimes = 150;
        this.lv.y = 150;
        this.setAnim('attack');
        this.rlv.linearVelocity = this.lv;
    },
    

    //被攻击状态
    isHurt(){
        if (this.isHit) {
            return;
        }
        this.isHit = true;
        this.enemyState = State.hurt;
        this.setAnim('hurt');
        if (this.hp >0) {
            this.hp--;
            this.bar.progress = this.hp / this.maxHP;
         //  console.log(this.hp); 
        }
        this.lv = this.rlv.linearVelocity;
        this.lv.x = 100;
        this.rlv.linearVelocity = this.lv;
        //this.emAnim.play('hurt');
        
        // var self = this;
        
     },
     //死亡状态
     death(){
    
        if (this.hp >0) {
            return;
        }
        this.lv = this.rlv.linearVelocity;
        this.enemyState = State.die;

        if (this.node.scaleX == -1) {
            this.sp.x = 1;
            this.lv.x = this.sp.x * this.hitDown;
        
        }else if (this.node.scaleX == 1) {
            this.sp.x = -1;
            this.lv.x = this.sp.x * this.hitDown;
        
        }
        this.lv.y = 150;
        this.setAnim('die');
        this.rlv.linearVelocity = this.lv;
            
     },

    //移动状态
    move(){
     
        let scaleX = Math.abs(this.node.scaleX);
        this.lv = this.rlv.linearVelocity;
        
        if (this.moveLeft) {
            this.sp.x = -1;
            this.node.scaleX = -scaleX;
            this.setAnim('run');
            
        } else if (this.moveRight) {
            this.sp.x = 1;
            this.node.scaleX = scaleX;
           
            this.setAnim('run');
        } else{
            this.sp.x = 0;
            this.setAnim('idle');
        }

        if (this.sp.x) {
            this.lv.x = this.sp.x * this.speedX;
        }else{
            this.lv.x = 0;
        }

        this.rlv.linearVelocity = this.lv;
    },
    
    //行为设置
    enemyAction(tt){
        let rl_pos = this.rolesNode.position;
        let en_pos = this.node.position;
        let dis = cc.Vec2.distance(en_pos, rl_pos);
        if (dis <= 110 && this.hp != 0){
            // this.enemyState = State.stand;
            this.moveLeft = false;
            this.moveRight = false;
            if (this.enemyState != State.hurt) {
                setTimeout(() => {
                    if (this.hp == 0) {
                        return;
                    }
                    this.enemyState = State.attack;
                }, (tt+Math.random())*1500);
            }
            //this.enemyState = State.attack;
            
        }else if (dis<=300) {
            //console.log("追击");
            let v = rl_pos.sub(en_pos);
        //    console.log(v.x);
            if (v.x < 0) {
                this.moveLeft = true;
                this.moveRight = false;
            }else{
                this.moveLeft = false;
                this.moveRight = true;
            }
            this.enemyState = State.stand;
        }else{
            //console.log("静止");
            this.moveLeft = false;
            this.moveRight = false;
           // this.setAnim('idle');
            this.enemyState = State.stand;
        }
    },

    update (dt) {
        
      
        //console.log("dt",dt);
        //状态切换
        this.tt += dt;
        if (this.tt >= 2 && this.enemyState == State.stand) {
            this.enemyAction(dt);
            this.tt = 0;
        }

        if (this.enemyState == State.attack) {
            this.attack();
            if (this.actTimes > 0) {
                this.actTimes --;
            }
           
           
        }else if (this.enemyState == State.stand) {
            this.move();
        }
        //this.killNum;
        // if (this.hp == 0) {
           
        //     this.showKillAmo(this.num);
        // }

    },

    
    
});
